package site.assad.web;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/1/9 15:24
 * Description: 上传，下载文件控制器
 */
@Controller
public class UploadController {

    @RequestMapping("/uploadPage")
    public String gotoUploadPage(){
        return "/uploadPage";
    }

    //单文件上传
    @RequestMapping(value="/upload",method=RequestMethod.POST)
    public String uploadSingle(@RequestParam(value="uploadFile") MultipartFile file, Model model,HttpSession session) throws IOException {
        if(!file.isEmpty()){
            model.addAttribute("msg","Upload File Success");
            model.addAttribute("name","file name:"+file.getOriginalFilename());  //获取上传文件名
            model.addAttribute("size","file size:"+file.getSize()+"bytes");   //获取上传文件大小
//          String uploadpath = session.getServletContext().getRealPath("./upload/");
            String uploadpath = "/C:/Al-assad/workplace/workplace-IDEA/testSpring_mvc_upload/src/main/webapp/upload";  //由于我使用的是idea的内嵌tomcat，方便调试
            file.transferTo(new File(uploadpath,file.getOriginalFilename()));

        }else{
            model.addAttribute("msg","Upload File fail");
        }
        return "/result";
    }

    //多文件上传
   @RequestMapping(value="/uploadMultiply",method=RequestMethod.POST)
    public String uploadMultiply(@RequestParam MultipartFile[] files,Model model,HttpSession session) throws IOException {
        List<FileCommand> fileList = new ArrayList<>();
//       String uploadpath = session.getServletContext().getRealPath("./upload/");
       String uploadpath = "/C:/Al-assad/workplace/workplace-IDEA/testSpring_mvc_upload/src/main/webapp/upload";  //由于我使用的是idea的内嵌tomcat，方便调试
        for(MultipartFile file : files){
            fileList.add(new FileCommand(file.getOriginalFilename(),file.getSize()+"bytes"));
            file.transferTo(new File(uploadpath,file.getOriginalFilename()));
        }
        model.addAttribute("msg","Upload File Result");
        model.addAttribute("resultList",fileList);
        return "/result";
   }

   //文件下载
   @RequestMapping(value="/download")
    public ResponseEntity<byte[]> download() throws IOException {
       String downloadPath = "/C:/Al-assad/workplace/workplace-IDEA/testSpring_mvc_upload/src/main/webapp/upload/downloadtest.jpg";
        File file = new File(downloadPath);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachement",new String("你好啊少年.jpg".getBytes("UTF-8"),"iso-8859-1"));
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }




}
